# 打开 CSV 文件并创建 reader 对象
import csv
from decimal import Decimal

from functools import reduce

bottotallst = {}
with open('locustmap.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)

    # 循环读取每一行
    for row in reader:
        # 90%>1s及失败协议: 协议：[成功率或失败数目，90%响应时间]
        dic_problem = {}
        # 服务器区服
        severnum = row [ 1 ]
        filename = row [ 1 ]

        # 读取bot机器人日志
        tpmlst = [ ]  # 多个Total的TPM
        nine_ratelst = [ ]  # 多个Total的90%
        max_lst = [ ]  # 多个Total的最大响应
        avg_lst = [ ]  # 多个Total的平均响应
        success_lst = [ ]  # 多个Total的成功率
        with open(f'./alllogs/alllogs/{filename}.txt', 'r', encoding='utf-8') as file:
            # 逐行读取文本内容，并去除行末的换行符
            lines = [ line.rstrip('\n') for line in file ]
            for i in range(len(lines)):
                line = lines [ i ]
                s_lst = line.split('|')
                try:
                    # 查找90%>1s及失败协议，并存入dic_problem
                    key_protocol = s_lst [ 0 ].strip()
                    success_rate = float(s_lst [ 3 ].strip())

                    nine_rate = int(s_lst [ 7 ].strip())
                    if key_protocol == 'Total':
                        tpm = int(s_lst [ 2 ].strip())
                        maxnum = int(s_lst [ 5 ].strip())
                        avgnum = int(s_lst [ 6 ].strip())
                        tpmlst.append(tpm)
                        nine_ratelst.append(nine_rate)
                        max_lst.append(maxnum)
                        success_lst.append(success_rate)
                        avg_lst.append(avgnum)

                    elif success_rate < 99.9 or nine_rate > 1000:
                        if key_protocol == '10001':
                            continue
                        else:
                            dic_problem [ key_protocol ] = [ success_rate, nine_rate ]
                        # print(line)
                except:
                    pass
        bot_tps = "%.2f" % float(reduce(lambda x, y: x + y, tpmlst) / 60)
        bot_nine = max(nine_ratelst)
        bot_avg = max(avg_lst)
        bot_success = min(success_lst)
        bot_max = max(max_lst)
        # 读取locust机器人日志
        with open(f'./lorequest/{filename}.csv') as csvfile:
            loreader = csv.reader(csvfile)
            # 循环读取每一行
            for row in loreader:
                # 查找90%>1s及失败协议，并存入dic_problem
                if row [ 0 ] == 'Action':
                    fail_num = int(row [ 3 ].strip())
                    nine_rate = int(row [ 15 ].strip())
                    if fail_num > 0 or nine_rate > 1000:
                        if row [ 1 ] == 'ALoginCheckReq':
                            continue
                        else:
                            dic_problem [ row [ 1 ] ] = [ fail_num, nine_rate ]
                elif row [ 1 ] == 'Aggregated':
                    # bot机器人的统一和locust机器人的统计数据作汇总
                    bot_nine = max(nine_rate, bot_nine)
                    bot_tps = "%.2f" % (float(bot_tps) + float(row [ 9 ]))
                    bot_max = max(int(bot_max), int(row [ 7 ].strip()))
                    bot_avg = max(float(bot_avg), float(row [ 5 ]))
        # 打印有问题数据
        print('区服' + '\t' + severnum)
        print('问题协议', end='\t')
        for key in dic_problem.keys():
            print(key, end='\n')
        print('\n')

        # 打印有问题数据详细信息
        print('区服'+'\t'+severnum)
        print('90%>1s及失败协议: 协议：[成功率或失败数目，90%响应时间]')
        print(dic_problem)
        print('\n')

        # # 打印请求数据
        # print(severnum, '\t', bot_tps, '\t', bot_success, '\t', bot_nine, '\t', bot_max, '\t', bot_avg)
